package cn.onlineclass.service;

import cn.onlineclass.dao.VisitMapper;
import cn.onlineclass.entity.OVisit;
import cn.onlineclass.vo.AjaxPage;
import cn.onlineclass.vo.AjaxResult;
import cn.onlineclass.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class VisitService {
    public static final VisitService INST=new VisitService();

    public AjaxResult update(OVisit visit){
        SqlSession session = MybatisUtil.get();
        try {
            VisitMapper mapper = session.getMapper(VisitMapper.class);
            return AjaxResult.success(mapper.update(visit));
        }finally {
            MybatisUtil.close(session);
        }
    }

    public AjaxResult add(OVisit visit){
        SqlSession session = MybatisUtil.get();
        try {
            VisitMapper mapper = session.getMapper(VisitMapper.class);
            return AjaxResult.success(mapper.add(visit));
        }finally {
            MybatisUtil.close(session);
        }
    }

    public AjaxResult del(long id){
        SqlSession session = MybatisUtil.get();
        try {
            VisitMapper mapper = session.getMapper(VisitMapper.class);
            return AjaxResult.success(mapper.del(id));
        }finally {
            MybatisUtil.close(session);
        }
    }
    public AjaxPage search(int pageNo, int pageSize){
        SqlSession s = MybatisUtil.get();
        VisitMapper mapper = s.getMapper(VisitMapper.class);
        try {
            List<OVisit> search = mapper.search(pageNo,pageSize);
            int total = mapper.searchTotal();
            return new AjaxPage(search,total,10,pageSize);
        }finally {
            MybatisUtil.close(s);
        }
    }

    public int searchTotal(){
        SqlSession session = MybatisUtil.get();
        try {
            VisitMapper mapper = session.getMapper(VisitMapper.class);
            return mapper.searchTotal();
        }finally {
            MybatisUtil.close(session);
        }
    }

    public AjaxResult get(long id){
        SqlSession session = MybatisUtil.get();
        try {
            VisitMapper mapper = session.getMapper(VisitMapper.class);
            return AjaxResult.success(mapper.get(id));
        }finally {
            MybatisUtil.close(session);
        }
    }
}
